﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using BoolanElement;
using BoolanSlide.FileStruct;

namespace BoolanSlide.Document.Format.Helper
{
    public static class BooSlideHelper
    {
        public static BooSlide GenerateSlide(XDocument doc)
        {
            BooSlide slide = new BooSlide();

            IEnumerable<XElement> results = doc.Element("BoolanPPT").Elements();
            foreach (XElement element in results)
            {
                Type type = null;
                foreach (Type item in BooDocumentHelper.ElementNames)
                {
                    if (item.Name == element.Name)
                    {
                        type = item;
                        break;
                    }
                }
                if (type == null)
                    break;
                BooElement elem = (BooElement)System.Activator.CreateInstance(type);
                elem.Parent = slide;
                elem.LoadFromXElement(element);
                slide.Elements.Add(elem);
            }

            slide.Initialize();

            if (doc.Element("BoolanPPT").Attribute("Background") != null)
                slide.BackgroundImagePath = doc.Element("BoolanPPT").Attribute("Background").Value;
            if (doc.Element("BoolanPPT").Attribute("DefaultFontColor") != null)
            {
                Int32 data = Int32.Parse(doc.Element("BoolanPPT").Attribute("DefaultFontColor").Value, System.Globalization.NumberStyles.HexNumber);
                slide.DefaultFontColor = BooUtility.FromHexValue((uint)data);
            }
            return slide;
        }
    }
}
